//
//  PhotoTransition.m
//  Browser
//
//  Created by lizp on 2017/7/16.
//  Copyright © 2017年 ZHY. All rights reserved.
//

#import "PhotoAnimation.h"


@interface PhotoAnimation()<UIViewControllerAnimatedTransitioning>

@property (nonatomic,assign) BOOL isPresented;

@end

@implementation PhotoAnimation

#pragma mark - UIViewControllerTransitioningDelegate

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.isPresented = YES;
    return self;
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.isPresented = NO;
    return self;
}

#pragma mark - UIViewControllerAnimatedTransitioning
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.25;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

    self.isPresented ? [self animatePresentedTransition:transitionContext]:[self animateDismissTransition:transitionContext];
    
    
}


#pragma mark - private
//弹出动画
-(void)animatePresentedTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
    [transitionContext.containerView addSubview:presentedView];
    
//    if (self.sourceImageView) {
//        UIImageView *imageView = self.sourceImageView;
//        [transitionContext.containerView addSubview:imageView];
//        imageView.frame = [self getStartAnimationRectWithImg:self.sourceImageView];
//        CGRect endRect = imageView.frame;
//        
//        let imageView = presentedDelegate.imageView(indexPath: indexPath)
//        transitionContext.containerView.addSubview(imageView)
//        imageView.frame = presentedDelegate.startRect(indexPath: indexPath)
//        let endRect = presentedDelegate.endRect(indexPath: indexPath)
//        
//        presentedView.alpha = 0.0;
//        transitionContext.containerView.backgroundColor = UIColor.black
//        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
//            imageView.frame = endRect
//        }) { (_) in
//            presentedView.alpha = 1.0
//            imageView.removeFromSuperview()
//            transitionContext.containerView.backgroundColor = UIColor.clear
//            transitionContext.completeTransition(true)
//        }
//    }
    
}

//消失动画
-(void)animateDismissTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
}


#pragma mark - private 
//获取初始动画的位置
-(CGRect)getStartAnimationRectWithImg:(UIImageView *)img {
    return  [self.sourceImageView convertRect:self.sourceImageView.frame toView:[UIApplication sharedApplication].keyWindow];
}

@end
